package javapop.framework.parser;

import javapop.framework.ParseContext;
import javapop.framework.ParseError;
import javapop.framework.ParseInput;
import javapop.framework.Parser;

public class ForceError extends Parser<Object> {
	private String message;
	private String description;
	private boolean fatal;
	
	public ForceError(String message, boolean fatal) {
		if(message==null) {
			throw new IllegalArgumentException("Error message is <null>");
		}
		this.message = message;
		this.fatal = fatal;
		description = null;
	}
	
	@Override
	public boolean isMemoized() {
		return false;
	}

	@Override
	public String getDescription() {
		if(description==null) {
			StringBuilder buf = new StringBuilder();
			if(fatal) {
				buf.append("Fatal");
			}
			buf.append("Error(\"");
			buf.append(message);
			buf.append("\")");
			description = buf.toString();
		}
		return description;
	}
	
	@Override
	public ParseError eval(ParseContext<?> ctx, ParseInput input) {
		return new ParseError(message,fatal);
	}

}
